import permissionApi from '@/api/permissionApi';
import {getToken} from '@/utils/cookie';

const btnPermission = {
  state: {
    btnPermissionList: []
  },
  mutations: {
    setBtnPermissionList: (state, permissionList) => {
      state.btnPermissionList = permissionList;
    }
  },
  actions: {
    GetBtnPermissions ({ commit, state }) {
      if (!state.btnPermission || state.btnPermission.length === 0) {
        return new Promise(resolve => {
          if (getToken()) {
            permissionApi.getPermission().then(res => {
              if (res) {
                commit('setBtnPermissionList', res);
                resolve(true);
              } else {
                resolve(false);
              }
            }).catch(() => resolve(false));
          }
        });
      }
    }
  }
}

export default btnPermission;
